using UnityEngine;
using QFramework;

namespace Projectsurviva
{
    public partial class CameraContoller : ViewController
    {
        private Vector2 mytargetpos = Vector2.zero;
        static CameraContoller defaultcamera;
        private int mshakeframe = 0;
        public static Transform Lbtrans => defaultcamera.LB;
        public static Transform Rttrans => defaultcamera.Rt;
        private void Awake()
        {
            defaultcamera = this;
        }

        private void OnDestroy()
        {
            defaultcamera = null;
        }
        void Start()
        {
            // Code Here
        }
        private Vector3 mcurrentpos;
        private bool mshake = false;
        private float mashakA = 0.5f;

        public static void Shake()
        {
            defaultcamera.mshake = true;
            defaultcamera.mshakeframe = 30;
            defaultcamera.mashakA = 0.2f;
        }
        private void Update()
        {
            if (Play.Default)
            {
                mytargetpos = Play.Default.transform.position;
                mcurrentpos.x = Mathf.Exp(1.0f - (-Time.deltaTime * 10)).Lerp(transform.position.x, mytargetpos.x);
                mcurrentpos.y = Mathf.Exp(1.0f - (-Time.deltaTime * 10)).Lerp(transform.position.y, mytargetpos.y);
                mcurrentpos.z = transform.position.z;
                if (mshake)
                {
                    mshakeframe--;
                    var shakeA = Mathf.Lerp(mashakA, 0, mshakeframe / 30);
                    transform.position = new Vector3(mcurrentpos.x + UnityEngine.Random.Range(-shakeA, shakeA), mcurrentpos.y + UnityEngine.Random.Range(-shakeA, shakeA), mcurrentpos.z);
                    if (mshakeframe == 0)
                    {
                        mshake = false;
                    }
                }
                else
                {
                    transform.PositionX(Mathf.Exp(1.0f - (-Time.deltaTime * 10)).Lerp(transform.position.x, mytargetpos.x));
                    transform.PositionY(Mathf.Exp(1.0f - (-Time.deltaTime * 10)).Lerp(transform.position.y, mytargetpos.y));
                }

            }
        }
    }
}
